extends Node2D


# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var spr: Sprite;

var texture:ImageTexture;
var spr_X:float = 200;
var spr_Y:float = 200;
var spr_xv: float = 3;
var spr_yv: float = 3;


var knight: Sprite;
var knightAniIndex : float = 10;
var knightAniNum : int = 20;

var idleImg = preload("res://assets/player/Idle (1).png");

var run1Img = preload("res://assets/player/Run (1).png");
var run2Img = preload("res://assets/player/Run (2).png");
var run3Img = preload("res://assets/player/Run (3).png");
var run4Img = preload("res://assets/player/Run (4).png");
var run5Img = preload("res://assets/player/Run (5).png");
var run6Img = preload("res://assets/player/Run (6).png");
var run7Img = preload("res://assets/player/Run (7).png");
var run8Img = preload("res://assets/player/Run (8).png");
var run9Img = preload("res://assets/player/Run (9).png");
var run10Img = preload("res://assets/player/Run (10).png");

var imgIndex: float = 0;

var player:AnimatedSprite;
const PLAYER_IDLE = "idle";
const PLAYER_RUN = "run";

var smallPlayer:Sprite;
var smallFrameIndex:float = 0;
var smallHorFrameIndex:float = 0;
var smallVecFrameIndex:int = 0;


# Called when the node enters the scene tree for the first time.
func _ready():
	print("ready")
	# inputInfo()
	# initSpr();
	# createKnight()
	# createKnight2();
	# createPlayer();
	createSmall();

# 帧定时函数
func _process(delta):
	# updateSprXy(delta);
	# updateKnightAni();
	# updateKnigh2();
	# updatePlayer();
	updateSmall()
	pass
	
# 单次按键信息
func _input(event):
	# 打印按键信息
	# print(event.as_text());
	# changeSpr(event);
	pass

func initSpr():
	# 添加到当前场景
	spr = Sprite.new();
	self.add_child(spr);
	
	# 设置位置
	spr.position = Vector2(spr_X,spr_Y);

	# 设置图形
	texture = ImageTexture.new();
	var image = Image.new();
	image.load("res://icon.png");
	texture.create_from_image(image)
	spr.texture = texture;
	
func updateSprXy(delta:float):
	spr_X = spr_X + delta * spr_xv;
	spr_Y = spr_Y + delta * spr_yv;

	spr.position = Vector2(spr_X,spr_Y);

func inputInfo():
	var actionArray = InputMap.get_actions();
	for a in actionArray:
		print(a);
		var actionEvents:Array = InputMap.get_action_list(a);
		var evts = [];
		for e in actionEvents:
			evts.append(e.as_text());

		print("		",evts);

func changeSpr(evt:InputEvent):
	if(evt is InputEventKey and evt.is_pressed()):
		onKey(evt.scancode)
	elif(evt is InputEventMouseButton and evt.is_pressed()):
		onMouse(evt)

# W,A,S,D按键处理
func onKey(keyCode):
	if(keyCode == KEY_A):
		spr_X = spr_X - spr_xv
	elif(keyCode == KEY_D):
		spr_X = spr_X + spr_xv
	elif(keyCode == KEY_W):
		spr_Y = spr_Y - spr_yv
	elif(keyCode == KEY_S):
		spr_Y = spr_Y + spr_yv

	spr.position = Vector2(spr_X,spr_Y);

# 跳转到鼠标位置
func onMouse(mouseEvt):
	print("鼠标按键",mouseEvt)
	if(mouseEvt.button_index == BUTTON_LEFT):
		# print(mouseEvt.position);
		# print(mouseEvt.global_position)
		spr.position = mouseEvt.position;





func createKnight():
	# 640 x 154 
	# 64 x 78
	knight = Sprite.new();
	self.add_child(knight);
	knight.position = Vector2(spr_X,300);

	# var text = ImageTexture.new();
	# var image = Image.new();
	# image.load("res://knight.png")
	# text.create_from_image(image);
	# knight.texture = text;
	knight.texture = load("res://knight.png")
	# 自动切割
	knight.hframes = 10;
	knight.vframes = 2;	

	knight.frame = knightAniIndex;

func updateKnightAni():
	# 更新索引
	if(floor(knightAniIndex) == knightAniNum -1):
		knightAniIndex = 10
	else:
		knightAniIndex = knightAniIndex + 0.3
	
	print(knightAniIndex,floor(knightAniIndex))
	knight.frame = floor(knightAniIndex);

	if(spr_X >= 800):
		spr_X = 0
	else:
		spr_X = spr_X + 5;
	
	knight.position = Vector2(spr_X,300);

func createKnight2():
	knight = Sprite.new();
	self.add_child(knight);
	knight.position = Vector2(100,400);

	knight.texture = idleImg

func updateKnigh2():
	var imgArray = [
		run1Img,run2Img,run3Img,
		run4Img,run5Img,run6Img,
		run7Img,run8Img,run9Img,
		run10Img
	];

	if(floor(imgIndex) == imgArray.size() - 1):
		imgIndex = 0
	else:
		imgIndex = imgIndex + 0.33;
	
	knight.texture = imgArray[floor(imgIndex)];


func createPlayer():
	# sprframe
	var sprframe = SpriteFrames.new();
	
	sprframe.add_animation(PLAYER_IDLE);
	sprframe.add_frame(PLAYER_IDLE,idleImg,0);

	sprframe.add_animation(PLAYER_RUN);
	var imgArray = [
		run1Img,run2Img,run3Img,
		run4Img,run5Img,run6Img,
		run7Img,run8Img,run9Img,
		run10Img
	];
	var i = 0;
	for img in imgArray:
		print("i",i)
		sprframe.add_frame(PLAYER_RUN,img,i)
		i = i + 1

	sprframe.set_animation_loop(PLAYER_RUN,true);
	sprframe.set_animation_speed(PLAYER_RUN,20);

	player = AnimatedSprite.new();
	self.add_child(player);
	player.position = Vector2(100,400); 
	player.frames = sprframe;
	player.animation = PLAYER_IDLE;
	player.name = "smallPlayer";

	# print(get_node("./smallPlayer"));

	player.play(PLAYER_RUN);

func updatePlayer():
	
	if(Input.is_key_pressed(KEY_A) || Input.is_key_pressed(KEY_D)):
		var face:int = 0;	
		if(Input.is_key_pressed(KEY_A)):
			spr_X = spr_X - spr_xv;
			face = -1;
		elif(Input.is_key_pressed(KEY_D)):
			spr_X = spr_X + spr_xv;
			face = 1;

		if(player.animation != PLAYER_RUN):
				player.animation = PLAYER_RUN;
		
		player.scale = Vector2(face,1);
	else:
		player.animation = PLAYER_IDLE;

	
	player.position = Vector2(spr_X,300);
	


func createSmall():
	smallPlayer = Sprite.new();
	self.add_child(smallPlayer);
	smallPlayer.position = Vector2(spr_X,spr_Y);

	smallPlayer.texture = load("res://player.png")
	# 自动切割
	smallPlayer.hframes = 5;
	smallPlayer.vframes = 4;	

	smallPlayer.frame_coords = Vector2(floor(smallHorFrameIndex),smallVecFrameIndex);


func updateSmall():
	var face = 0;
	var isMove = false;
	if(Input.is_key_pressed(KEY_A)):
		spr_X = spr_X - spr_xv;
		face = 1
		isMove = true
	elif(Input.is_key_pressed(KEY_D)):
		spr_X = spr_X + spr_xv;
		face = 2
		isMove = true
	elif(Input.is_key_pressed(KEY_W)):
		spr_Y = spr_Y - spr_yv;
		face =3
		isMove = true
	elif(Input.is_key_pressed(KEY_S)):
		spr_Y = spr_Y + spr_yv;
		face = 0
		isMove = true
	
	if(smallVecFrameIndex != face):
		smallHorFrameIndex = 0;
	else:
		if(isMove == true):
			if(floor(smallHorFrameIndex) == 4):
				smallHorFrameIndex = 0
			else:
				smallHorFrameIndex = smallHorFrameIndex + 0.3

	smallVecFrameIndex = face;
	smallPlayer.frame_coords = Vector2(floor(smallHorFrameIndex),smallVecFrameIndex);
	
	smallPlayer.position = Vector2(spr_X,spr_Y)
	pass
